##################
# China GFDI MA paper 
# Date: 2/1/2023
# Author: Aycan Katitas 
# Figures 
##################

##################
### setwd and load libraries 

setwd("~/Desktop/Git/chinagfdi")

library(dplyr)
library(tidyverse)
library(broom)
library(arm)
library(viridis)
library(readstata13)
library(concordance)
library(scales)

################# Load data 
# anes1220analysisIV.dta in Rds format
anesdistricttime2 <- readRDS("data/anes1220analysisIV.Rds")


################# Appendix Figures 

# FIGURE A1 - ANES Respondents' Attitudes Toward China, Broken Down by Survey Year

anesdistricttime2 %>% 
  mutate(chectg=ifelse(year==2012&chinaord==0,"Good",
                       ifelse(year==2012&chinaord==1,"Neither Good or Bad",
                              ifelse(year==2012&chinaord==2,"Bad",
                                     ifelse(year==2020&chinaord==0,"No Threat",
                                            ifelse(year==2020&chinaord==1,"Little Threat",
                                                   ifelse(year==2020&chinaord==2,"Moderate Threat",
                                                          ifelse(year==2020&chinaord==3,"A Lot of Threat",
                                                                 ifelse(year==2020&chinaord==4,"A Great Deal of Threat",NA)))))))),
         year=ifelse(year==2012,"ANES 2012","ANES 2020")) %>% 
  dplyr::select(caseid,year,chectg) %>%
  drop_na() %>% 
  mutate(chectg=fct_relevel(chectg,c("Good","Neither Good or Bad","Bad","No Threat",
                                     "Little Threat","Moderate Threat","A Lot of Threat","A Great Deal of Threat"))) %>%
  group_by(year,chectg) %>% 
  summarise(n=n()) %>% 
  group_by(year) %>% 
  mutate(perc=100*n/sum(n)) %>% 
  ggplot(aes(chectg,perc)) +
  geom_bar(stat="identity") +
  facet_wrap(~year,
             scales="free") + 
  theme_classic(base_size=66) +
  xlab("Is China a Threat?") +
  ylab("") + 
  theme(axis.text.x = element_text(angle = 45,hjust=1))

ggsave(plot=last_plot(),filename="figures/figa1.png",
       width = 45, height = 25, units = "in")

## FIGURE A2 - The Value of Chinese FDI Position and Greenfield Investment 2004-2020

chinaan2 <- readRDS("data/chinabopgfdima.Rds")


# figure bop gfdi 
chinaan2 %>%
  filter(group=="bop"|group=="val") %>% 
  ggplot(aes(x=Year,y=value,group=group,color=group)) + 
  geom_line(size=2) +
  scale_color_manual(values=c("black","green")) +
  ylab("$ (millions)") +
  theme_classic(base_size = 56) +
  theme(axis.title.y = element_text(vjust = 1, angle = 360,
                                    margin=margin(t=0,r=20,b=0,l=40)),
        legend.position="none") + 
  scale_x_continuous(breaks=seq(min(chinaan2$Year),max(chinaan2$Year),1)) +
  annotate("text",x=2019.5,y=37000,label="FDI Position",size=15) +
  annotate("text",x=2019.5,y=1000,label="Greenfield FDI Value",size=15) +
  coord_cartesian(xlim = c(2004, 2020), 
                  clip = 'off')

ggsave(plot=last_plot(),filename="figures/figa2.png",
       width = 45, height = 25, units = "in",limitsize=FALSE) 

## FIGURE A3 - Chinese Greenfield Investment and Mergers and Acquisitions 2004-2020
chinaan2 %>%
  filter(group=="no"|group=="ma") %>% 
  ggplot(aes(x=Year,y=value,group=group,color=group)) + 
  geom_line(size=2) +
  scale_color_manual(values=c("red","green")) +
  ylab("$ (millions)") +
  theme_classic(base_size = 56) +
  theme(legend.position="none") + 
  scale_x_continuous(breaks=seq(min(chinaan2$Year),max(chinaan2$Year),1)) + 
  annotate("text",x=2020,y=60,label="Greenfield",size=15) +
  annotate("text",x=2020,y=40,label="M&A",size=15) +
  coord_cartesian(xlim = c(2004, 2020), 
                  clip = 'off') + 
  ylab("Number of Chinese Greenfield FDI/Mergers and Acquisitions")

ggsave(plot=last_plot(),filename="figures/figa3.png",
       width = 35, height = 25, units = "in",limitsize=FALSE) 

## FIGURE A4 - The Location of Chinese GFDI Announcements 2004-2020

cdmap <- readRDS("data/chinamap.Rds")

ggplot(cdmap,aes(fill=count))+
  geom_sf() +
  theme_void() + 
  scale_fill_distiller(palette = "Reds", 
                       direction = 1,name="",
                       breaks=c(0,25),
                       labels=c("Low","High")) +
  theme(legend.position="bottom",
        legend.key.size=unit(1,"cm"),
        legend.text = element_text(size=30))

ggsave("figures/figa4.png",width=6,height=6,dpi="screen")
#ggsave(plot=last_plot(),filename="figures/figa4.png",
#      width = 25, height = 25, units = "in",limitsize=FALSE) 



# FIGURE A5 - Predicted Probability of Perceiving China as a Threat as Jobs by Chinese FDI Increases, Interacted with ANES Survey Year

# Results generated in Tables.do
jobsyear <- read.csv("data/marginsjobsyear.csv")

jobsyear %>% 
  mutate(year=ifelse(year==2012,"ANES 2012","ANES 2020"),
         year=factor(year,levels=c("ANES 2012","ANES 2020"))) %>% 
  ggplot(aes(x=chinajobsiv_d_log,y=estimate*100,ymin=conflow*100,ymax=confhigh*100,fill=year)) +
  geom_ribbon() + 
  geom_line(aes(linetype=year),color="black",size=4) + 
  #geom_hline(yintercept=50,linetype="dashed",size=2) +
  facet_wrap(~year) +
  ylab("Pr(China as a Threat)") +
  xlab("Chinese GFDI Jobs (logged)") +
  theme_classic(base_size=66) +
  scale_linetype_manual(values=c("solid","solid")) + 
  scale_fill_manual(values=c("azure3","azure4")) + 
  guides(fill="none")  +
  theme(legend.position="none") + 
  scale_x_continuous(breaks=seq(min(jobsyear$chinajobsiv_d_log),max(jobsyear$chinajobsiv_d_log),1)) + 
  ylim(0,75)

ggsave(plot=last_plot(),filename="figures/figa5.png",
       width = 45, height = 25, units = "in")

# FIGURE A6 - The Interaction Between Chinese Greenfield FDI Jobs and Nationalism

# Results generated in Tables.do
natjobs <- read.csv("data/natmargins.csv")

natjobs %>% 
  mutate(nationalism=ifelse(nationalism==0,"Low",
                            ifelse(nationalism==2,"Medium","High")),
         nationalism=factor(nationalism, levels=c("Low","Medium","High"))) %>% 
  ggplot(aes(x=chinajobsiv_d_log,y=est*100,ymin=conflow*100,ymax=confhigh*100,fill=nationalism)) +
  geom_ribbon() + 
  geom_line(aes(linetype=nationalism),color="black",size=4) + 
  facet_wrap(~nationalism) +
  ylab("Pr(China as a Threat)") +
  xlab("Chinese GFDI Jobs (logged)") +
  theme_classic(base_size=66) +
  scale_linetype_manual(values=c("solid","solid","solid")) + 
  scale_fill_manual(values=c("azure3","azure3","azure3")) + 
  guides(fill="none")  +
  theme(legend.position="none") + 
  scale_x_continuous(breaks=seq(min(natjobs$chinajobsiv_d_log),max(natjobs$chinajobsiv_d_log),1))

ggsave(plot=last_plot(),filename="figures/figa6.png",
       width = 45, height = 25, units = "in")

## FIGURE A7 - The Share of Greenfield and M&A Projects by Industry 2003-2020

indgraph <- readRDS("data/chinamagfdiind.Rds")

indgraph %>%  
  filter(!is.na(indname)) %>% 
  mutate(indname=ifelse(indname=="Administrative and Support and Waste Management and Remediation Services",
                        "Administrative/Support/Waste Management/Remediation Services",
                        ifelse(indname=="Professional, Scientific, and Technical Services","Scientific/Technical Services",
                               indname))) %>% 
  ggplot(aes(reorder(indname,-per),per,fill=group)) +
  geom_bar(stat="identity",position="dodge") + 
  theme_classic(base_size = 56) +
  theme(legend.title=element_blank(),
        legend.position="bottom",
        axis.text.x=element_text(angle = 45, vjust = 1, hjust=1)) + 
  xlab("") + 
  ylab("%") + 
  scale_fill_manual(values=c("green","red"), 
                    labels=c("Greenfield","M&A"))

ggsave(plot=last_plot(),filename="figures/figa7.png",
       width = 40, height = 35, units = "in") 

## FIGURE A8 - The Share of Critical Industries among Greenfield and M&A Projects 2003-2020

indgraph2 <- readRDS("data/chinaindgraphnatsec.Rds")

indgraph2 %>%  
  ggplot(aes(reorder(natsec,-per),per,fill=group)) +
  geom_bar(stat="identity",position="dodge") + 
  theme_classic(base_size = 56) + 
  theme(legend.title=element_blank(),
        legend.position="bottom") + 
  xlab("") + 
  ylab("%") +
  scale_fill_manual(values=c("green", "red"), 
                    labels=c("Greenfield","M&A")) +
  scale_x_discrete(labels=c("Not Sensitive Industry","Sensitive Industry"))

ggsave(plot=last_plot(),filename="figures/figa8.png",
       width = 20, height = 15, units = "in") 




